<?php

class My_Html_FilterList
{

    /**
     * @var string
     */
    protected $id;
    /**
     * @var string
     */
    protected $title;
    /**
     * @var array
     */
    protected $items = array();
    /**
     * @var Zend_View
     */
    protected $view;
    /**
     * @var string
     */
    protected $renderScript = 'default.phtml';

    protected function __construct($id)
    {
        $this->id = $id;
        $this->view = new Zend_View();
        $this->view->addScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'scripts'.DIRECTORY_SEPARATOR.'filter-list');
        $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
    }

    /**
     * @param string $id
     * @return My_Html_FilterList
     */
    public static function factory($id = null)
    {
        if (is_null($id)) $id = uniqid();
        return new self($id);
    }

    /**
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $id
     * @return My_Html_FilterList
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param string $title
     * @return My_Html_FilterList
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getItems()
    {
        return $this->items;
    }

    /**
     * @param array $items
     * @return My_Html_FilterList
     */
    public function setItems($items)
    {
        $this->items = $items;
        return $this;
    }
    
    /**
     * @param string $item
     * @return My_Html_FilterList
     */
    public function addItem($item)
    {
        $this->items[] = $item;
        return $this;
    }
    
    /**
     * @return Zend_View
     */
    public function getView()
    {
        return $this->view;
    }

    /**
     * @param Zend_View $view
     * @return My_Html_FilterList
     */
    public function setView($view)
    {
        $this->view = $view;
        return $this;
    }

    /**
     * @return string
     */
    public function getRenderScript()
    {
        $renderScript = $this->renderScript;
        return $renderScript;
    }

    /**
     * @param string $renderScript
     * @return My_Html_FilterList
     */
    public function setRenderScript($renderScript)
    {
        $this->renderScript = $renderScript;
        return $this;
    }

    /**
     * @return string
     */
    public function render()
    {
        $view = $this->getView();
        $view->list = $this;
        return $view->render($this->getRenderScript());
    }

    /**
     * @return string
     */
    public function __toString()
    {
        try
        {
            $htmlCode = $this->render();
        }
        catch (Exception $e)
        {
            $htmlCode = sprintf('Error rendering filter list %s: %s (%s).', $this->getId(), $this->getTitle(), $e->getMessage());
        }
        return $htmlCode;
    }

}
